package com.wdg.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.wdg.pojo.Login_log;
import com.wdg.dao.LogDao;
import com.wdg.util.DBUtil;

public class LogDaoImpl implements LogDao {
	Connection conn = DBUtil.getCn();
	ResultSet rs = null;
	Statement st = null;
	PreparedStatement pst = null;

	@Override
	public void addlog(Login_log log) {
		String sql = "insert into login_log(log_time,log_ip,user_id,log_action) values(?,?,?,?)";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, log.getLog_time());
			pst.setString(2, log.getLog_ip());
			pst.setInt(3, log.getUser_id());
			pst.setString(4, log.getLog_action());
			pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public ArrayList<Login_log> selectLog() {
		ArrayList<Login_log> list = new ArrayList<>();
		String sql = "select reg_user.user,login_log.* from reg_user,login_log where login_log.user_id=reg_user.id ORDER BY id";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				Login_log log = new Login_log();
				log.setId(rs.getInt("login_log.id"));
				log.setLog_time(rs.getString("login_log.log_time"));
				log.setLog_ip(rs.getString("login_log.log_ip"));
				log.setUser_id(rs.getInt("login_log.user_id"));
				log.setLog_action(rs.getString("login_log.log_action"));
				log.setUser(rs.getString("reg_user.user"));
				list.add(log);
			}
		} catch (SQLException e) {
			e.printStackTrace();

		}
		return list;
	}
		// 删除方法（删日志）
	@Override
	public void delLog(int id) {
		String sql = "delete from login_log where user_id = ?";
		try {
			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setInt(1, id);
			psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}